package converter;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import repository.CategoryRepository;
import domain.Category;

@Component
public class CategoryConverter implements Converter<String, Category>{
	
	private CategoryRepository categoryRepository;
	
	@Autowired
	public CategoryConverter(CategoryRepository categoryRepository) {
		this.categoryRepository = categoryRepository;
	}

	@Override
	public Category convert(String source) {
		if(source.isEmpty()) {
			return null;
		}
		
		return categoryRepository.getCategory(Integer.valueOf(source));
	}

}
